<?php
class ImagesController extends AController
{

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('upload'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionUpload(){
		if(isset($_FILES['image'])){

			$image = Yii::app()->file->set('image');

			if(isset($_POST['width']) && intval($_POST['width']) > 0)
				WImage::thumbnailImage($image->realpath, intval($_POST['width']));
				
			$image->set(WImage::saveAsJPG($image->realpath));
			$dir = 'images/pages/'.date('Ym');
			Yii::app()->file->set($dir)->createDir();

			$new = $dir.'/'.uniqid().'.jpg';
			$image->copy($new);
			echo '/'.$new;
		}
	}
	
}